﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MVCCentral.Framework
{
    public class FileSizer
    {
        private static readonly long kilobyte = 1024;
        private static readonly long megabyte = 1024 * kilobyte;
        private static readonly long gigabyte = 1024 * megabyte;
        private static readonly long terabyte = 1024 * gigabyte;

        public static string ToByteString(long bytes)
        {
            if (bytes > terabyte) return (bytes / terabyte).ToString("0.00 TB");
            else if (bytes > gigabyte) return (bytes / gigabyte).ToString("0.00 GB");
            else if (bytes > megabyte) return (bytes / megabyte).ToString("0.00 MB");
            else if (bytes > kilobyte) return (bytes / kilobyte).ToString("0 KB");
            else return bytes + " Bytes";
        }

        public static string FileSize(long bytes)
        {
            return bytes.ToFileSize();
        }

        public static string GetFileSize(string filePath)
        {
            string result = string.Empty;

            FileInfo fi = new FileInfo(filePath);
            long size = fi.Length;
            //result = ToByteString(size);
            result = size.ToFileSize();
            return result;
        }
    }
}
